package com.ztools.pc.service.impl.information;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztools.common.model.pc.export.MemberExport;
import com.ztools.common.model.pc.information.WeChatMember;
import com.ztools.common.util.common.PageUtil;
import com.ztools.common.util.pc.DictUtil;
import com.ztools.pc.mapper.information.MemberMapper;
import com.ztools.pc.service.information.MemberService;
import com.ztools.pc.service.sys.DictService;
import com.ztools.pc.util.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:seeTools
 * @Date: 2023/4/14  20:21
 * @Version 1.0
 */
@Slf4j
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, WeChatMember> implements MemberService {

    @Resource
    private DictService dictService;
    @Resource
    private ExcelUtil excelUtil;

    @Override
    public Page<WeChatMember> findList(PageUtil<WeChatMember> pageUtil) {
        List<OrderItem> orders = new ArrayList<>();
        QueryWrapper<WeChatMember> queryWrapper = new QueryWrapper<WeChatMember>(pageUtil.getParams());
        Page<WeChatMember> pages = new Page<>(pageUtil.getPage(), pageUtil.getSize());
        // 排序数据  StrUtil.toUnderlineCase huTools将驼峰装换为下划线格式
        orders.add(new OrderItem(StrUtil.toUnderlineCase(pageUtil.getSortName()), "asc".equals(pageUtil.getSortOrder())));
        pages.setOrders(orders);
        Page<WeChatMember> page = page(pages, queryWrapper);
        dictionary(page.getRecords());
        return page;
    }

    @Override
    public boolean add(WeChatMember weChatMember) {
        return save(weChatMember);
    }

    @Override
    public boolean delete(WeChatMember weChatMember) {
        WeChatMember byId = getById(weChatMember);
        return removeById(weChatMember);
    }

    @Override
    public void exportExcel(HttpServletResponse response, WeChatMember weChatMember) {
        long count = count();
        long sheetNumber = ExcelUtil.SHEET_NUMBER;
        long pageSize = excelUtil.getPageSize(sheetNumber, count);
        QueryWrapper<WeChatMember> queryWrapper = new QueryWrapper<>(weChatMember);
        for (int i = 0; i < pageSize; i++) {
            Page<WeChatMember> pages = new Page<>(i, sheetNumber);
            Page<WeChatMember> page = page(pages, queryWrapper);
            List<WeChatMember> list = page.getRecords();
            dictionary(list);
            excelUtil.exportExcel(response, i, list, MemberExport.class, "公众号粉丝基本信息");
        }
    }

    @Override
    public List<WeChatMember> findByObject(WeChatMember weChatMember) {
        QueryWrapper<WeChatMember> queryWrapper = new QueryWrapper<>(weChatMember);
        return list(queryWrapper);
    }

    @Override
    public WeChatMember findByOpenId(String openId) {
        QueryWrapper<WeChatMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid", openId);
        return getOne(queryWrapper);
    }

    @Override
    public boolean update(WeChatMember weChatMember) {
        // 修改前先获取，在删除（乐观锁）
        WeChatMember memberById = getById(weChatMember);
        return updateById(weChatMember);
    }

    @Override
    public void updateNickName(Long memberId, String nickName) {
        UpdateWrapper<WeChatMember> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", memberId).set("nick_name", nickName);
        update(updateWrapper);
    }


    @Override
    public List<WeChatMember> findBy(String fieldName, Object value) {
        QueryWrapper<WeChatMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(fieldName, value);
        return list(queryWrapper);
    }


    /**
     * 字典翻译
     *
     * @param weChatMemberList 对象集合
     */
    public void dictionary(List<WeChatMember> weChatMemberList) {
        for (WeChatMember weChatMember : weChatMemberList) {
            dictionary(weChatMember);
        }
    }

    public void dictionary(WeChatMember weChatMember) {
        if (weChatMember != null) {
            // 性别翻译
            if (weChatMember.getSex() != null) {
                weChatMember.setSexName(dictService.findByTypeCode(DictUtil.SEX, weChatMember.getSex() + ""));
            }
            // 粉丝状态翻译
            if (weChatMember.getMemberStatus() != null) {
                weChatMember.setMemberStatusName(dictService.findByTypeCode(DictUtil.MEMBER_STATUS, weChatMember.getMemberStatus() + ""));
            }
        }
    }


}
